package com.bstek.urule.console;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Dbutil {

    public static final String URL = "jdbc:postgresql://pgm-rj9cayx4s9d7682c9o.pg.rds.aliyuncs.com:1921/loan_mexico_pro";
    public static final String USER = "loan_mexico_pro";
    public static final String PASSWORD = "!m12345678Ab";
    private static Connection conn = null;
    static{
        try {
            //1.加载驱动程序
            Class.forName("org.postgresql.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }


    public static void insert(UruleOperationLogEntity entity) throws SQLException {
        Connection conn = getConnection();

        String sql = "INSERT INTO urule_operation_log(id, operator_name, operation_type, operation_log, file_before_modification, file_after_modification,"+
                "server_ip, create_time, update_time)"
                +"values(nextval('urule_operation_log_id_seq'),?,?,?,?,?,?,current_timestamp,current_timestamp)";
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行
        ptmt.setString(1, entity.getOperatorName());
        ptmt.setString(2, entity.getOperationType().toString());
        ptmt.setString(3, entity.getOperationLog());
        ptmt.setString(4, entity.getFileBeforeModification());
        ptmt.setString(5, entity.getFileAfterModification());
        ptmt.setString(6, entity.getServerIp());
        //执行
        ptmt.execute();
    }
}
